﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Security;
using System.Text;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Win32;

namespace RegTreeView
{
    class RegLib
    {
        
    }

    class RegKey 
    {
        public RegKey() {}


        public RegKey(RegistryKey InRoot)
        {
            _IsRegKey = true;
            string[] NameItems = InRoot.Name.Split(new char[] { '\\' });
            _Name = NameItems[NameItems.Length - 1];
            _Hive = NameItems[0];
            _RegKeyPath = InRoot.Name;
            _RootKey = InRoot;
        }

        private string _Hive;
        public string Hive
        {
            get { return _Hive; }
            set { _Hive = value; }
        }

        private RegistryKey _RootKey;
        public RegistryKey RootKey
        {
            get { return _RootKey; }
            set { _RootKey = value; }
        }

        private string _RegKeyPath;
        public string RegKeyPath
        {
            get { return _RegKeyPath; }
            set { _RegKeyPath = value; }
        }

        private string _Name;
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        private bool _IsRegKey;
        public bool IsRegKey
        {
            get { return _IsRegKey; }
            set { _IsRegKey = value; }
        }

        private string _Value;
        public string Value
        {
            get { return _Value; }
            set { _Value = value; }
        }
        

        public IEnumerable<RegKey> Children
        {
            get
            {
                if (this.IsRegKey)
                {
                    string[] SubKeyNames = _RootKey.GetSubKeyNames();

                    var SortedKeys = from Key2Sorted in SubKeyNames
                                     orderby Key2Sorted ascending
                                     select Key2Sorted;


                    foreach (var name in SortedKeys)
                    {
                        RegKey rtnKey = new RegKey();
                        try
                        {
                            RegistryKey CurKey = _RootKey.OpenSubKey(name);
                            rtnKey.IsRegKey = true;

                            string[] NameItems = CurKey.Name.Split(new char[] { '\\' });

                            rtnKey.RegKeyPath = CurKey.Name;

                            rtnKey.Name = NameItems[NameItems.Length - 1];

                            rtnKey.Hive = NameItems[0];

                            rtnKey.RootKey = CurKey;
                        }
                        catch (SecurityException)
                        {
                            continue;
                        }

                        catch (Exception Ex)
                        {
                            throw Ex;
                        }

                        if (rtnKey != null)
                            yield return rtnKey;
                    }

                    string[] ValueNames = _RootKey.GetValueNames();

                    var SortedValues = from Key2Sorted in ValueNames
                                       orderby Key2Sorted ascending
                                       select Key2Sorted;

                    foreach (var name in SortedValues)
                    {

                        if (name != "")
                        {
                            RegKey rtnKey = new RegKey();

                            rtnKey.IsRegKey = false;

                            rtnKey.Name = name;

                            rtnKey.Value = _RootKey.GetValue(name).ToString();

                            string[] NameItems = _RootKey.Name.Split(new char[] { '\\' });
                            rtnKey.Hive = NameItems[0];

                            rtnKey._RegKeyPath = _RootKey.Name;

                            rtnKey.RootKey = _RootKey;

                            yield return rtnKey;
                        }
                        else
                            continue;
                    }
                }
            }
        }

    

    }


    [ValueConversion(typeof(object), typeof(BitmapImage))]
    public class RegItemConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                System.Globalization.CultureInfo culture)
        {
            string sImagePath = "";

            if ((bool) value)
            {
                sImagePath = @"Images/folder.png";
            }
            else
            {
                sImagePath = @"Images/cog.png";
            }

            BitmapImage rtnImage = new BitmapImage(new Uri(sImagePath, UriKind.Relative));


            return rtnImage;
        }

       

        public object ConvertBack(object value, Type targetType, object parameter,
                    System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    


}
